use std::sync::mpsc::Receiver;
use std::sync::{self, mpsc, Arc, Mutex, RwLock};
use std::thread;

use tokio::runtime::Runtime;
use tokio::time::{interval, Duration};

/**
 * 创建一个计时器
 */
pub fn create_timing<F>(f: F)
where
    F: Fn(u32) -> (),
{
    let rt = Runtime::new().unwrap();
    rt.block_on(timing(f));
}

async fn timing<F>(f: F)
where
    F: Fn(u32) -> (),
{
    let mut interval = interval(Duration::from_secs(1));
    let mut temp = 0;
    loop {
        interval.tick().await;
        temp += 1;
        f(temp)
    }
}

fn box_test(){

}


#[derive()]
pub struct Timing {
    // 是否启动
    if_loop: bool,
    // 间隔时间，毫秒级别
    space_time: u64,
}

impl Timing {
    pub fn new(space_time: u64) -> Timing {
        return Timing {
            if_loop: true,
            space_time,
        };
    }

    pub fn create<F>(&self, f: F)
    where
        F: Fn(u64) -> (),
    {
        let rt = Runtime::new().unwrap();
        let mut temp: u64 = 0;
        rt.block_on(async {
            let mut interval = interval(Duration::from_millis(self.space_time));

            loop {
               
                interval.tick().await;
                temp += 1;

                f(temp * self.space_time);
            }
        });
    }

    pub fn pause(&mut self) {
        println!("修改了 pause");
       self.if_loop = true;
    }

    pub fn get_loop(&self) -> bool{
        self.if_loop
    }

}
